package com.example.learningnote.views;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.learningnote.R;
import com.example.learningnote.response.UserResponse;

public class UserInfoFragment extends Fragment {
    private TextView tokenTextView;
    private TextView usernameTextView;
    private ImageView avatarImageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_info, container, false);
        tokenTextView = view.findViewById(R.id.user_token);
        usernameTextView = view.findViewById(R.id.user_name);
        avatarImageView = view.findViewById(R.id.user_avatar);
        return view;
    }

    public void updateUserInfo(UserResponse userResponse) {
        tokenTextView.setText(userResponse.getToken());
        usernameTextView.setText(userResponse.getUsername());
        // 使用 Glide 加载头像
        Glide.with(this)
                .load(userResponse.getAvatar())
                .placeholder(R.drawable.test) // 占位图
                .error(R.drawable.test) // 错误图
                .diskCacheStrategy(DiskCacheStrategy.ALL) // 缓存策略
                .into(avatarImageView);
    }

    public void showError(String error) {
        tokenTextView.setText(error);
        usernameTextView.setText("");
        avatarImageView.setImageDrawable(null);
    }
}

